package cn.yr.blog.controller.management;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.blog.service.ArticleService;
import cn.yr.blog.service.CommentService;
import cn.yr.common.core.domain.entity.YrArticle;
import cn.yr.common.core.domain.entity.YrComment;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import cn.yr.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 评论
 * @author Lang 1102076808@qq.com
 * @date 2020-08-22 10:51:36
 */
@Api(tags = "management评论")
@RestController
@RequestMapping("/management/comment")
public class ManagerCommentController {

	@Autowired
	CommentService commentService;

	@Autowired
	ArticleService articleService;
	/**
	 * 分页列表
	 */
	@RequestMapping("/page")
	@PreAuthorize(hasPermi = "manager:comment:page")
	public Ret page(@RequestBody PageVo<YrComment> pageVo){

		QueryWrapper<YrComment> wrapper = new QueryWrapper<>();
		wrapper.like(ObjectUtil.isNotEmpty(pageVo.getParams().get("content")),"content",pageVo.getParams().get("content"));
		wrapper.like(ObjectUtil.isNotEmpty(pageVo.getParams().get("senderName")),"sender_name",pageVo.getParams().get("senderName"));
		wrapper.like(ObjectUtil.isNotEmpty(pageVo.getParams().get("receiverName")),"receiver_name",pageVo.getParams().get("receiverName"));
		wrapper.eq(ObjectUtil.isNotEmpty(pageVo.getParams().get("articleId")),"article_id",pageVo.getParams().get("articleId"));
		wrapper.eq(ObjectUtil.isNotEmpty(pageVo.getParams().get("isReview")),"is_review",pageVo.getParams().get("isReview"));
		wrapper.orderByAsc("is_review");
		wrapper.orderByDesc("create_time");
		PageVo<YrComment> pageYrComment = commentService.page(pageVo,wrapper);
		setCommentOfArticle(pageYrComment.getRecords());
		return Ret.success(pageYrComment);
	}

	private void setCommentOfArticle(List<YrComment> list){
		if(ObjectUtil.isEmpty(list)){
			return;
		}
		List<Integer> ids = list.stream().map(YrComment::getArticleId).collect(Collectors.toList());
		List<YrArticle> yrArticles = articleService.listByIds(ids);
		Map<Integer, YrArticle> map = yrArticles.stream().collect(Collectors.toMap(YrArticle::getId, article -> article));
		list.forEach(comment->comment.setYrArticle(map.get(comment.getArticleId())));
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@PreAuthorize(hasPermi = "manager:comment:list")
	public Ret list(){
		return Ret.success(commentService.list());
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@PreAuthorize(hasPermi = "manager:comment:info")
	public Ret info(@PathVariable("id") Integer id){
		YrComment  yrComment = commentService.getById(id);
		return Ret.success(yrComment);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/add")
	@PreAuthorize(hasPermi = "manager:comment:add")
	public Ret save(@Validated @RequestBody YrComment yrComment){
		commentService.save(yrComment);
		return Ret.success();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/edit")
	@PreAuthorize(hasPermi = "manager:comment:edit")
	public Ret update(@Validated @RequestBody YrComment yrComment){
		commentService.updateById(yrComment);
		return Ret.success();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/remove/{ids}")
	@PreAuthorize(hasPermi = "manager:comment:remove")
	public Ret delete(@PathVariable Integer[] ids){
		commentService.removeByIds(Arrays.asList(ids));
		return Ret.success();
	}

}
